//
//  DateUtil.m
//  mPOPeasy
//
//  Created by 정건국 on 10. 8. 17..
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DateUtil.h"


@implementation DateUtil

+ (NSString*)formatHHMMSSFromDateStr:(NSString*)dateStr{
	if([dateStr length] != 6)
		return dateStr;
	NSString * hour = [dateStr substringToIndex:2];
	NSRange range;
	range.location = 2;
	range.length = 2;
	NSString * minute = [dateStr substringWithRange:range];
	range.location = 4;
	NSString * second = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second];
}

+ (NSString*)formatYYMMDDFromDateStr:(NSString*)dateStr{
	if([dateStr length] != 8)
		return dateStr;
	NSString * year = [dateStr substringToIndex:4];
	NSRange range;
	range.location = 4;
	range.length = 2;
	NSString * month = [dateStr substringWithRange:range];
	range.location = 6;
	NSString * day = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@/%@/%@", year, month, day];
}

+ (NSString*)formatYYMMDDFromDateStrWithSeperator:(NSString*)dateStr seperator:(NSString*)seperator{
	if([dateStr length] != 8)
		return dateStr;
	NSString * year = [dateStr substringToIndex:4];
	NSRange range;
	range.location = 4;
	range.length = 2;
	NSString * month = [dateStr substringWithRange:range];
	range.location = 6;
	NSString * day = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@%@%@%@%@", year, seperator, month, seperator, day];
}

+ (NSString *)formatMMDDFromDateStr:(NSString *)dateStr {
	// YYYYMMDDHHMISS
	if([dateStr length] != 14)
		return dateStr;
	NSRange range;
	range.location = 4;
	range.length = 2;
	NSString * month = [dateStr substringWithRange:range];
	range.location = 6;
	NSString * day = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@/%@", month, day];
}

+ (NSString *)formatFullFromDateStr:(NSString *)dateStr {
	// YYYYMMDDHHMISS
	if([dateStr length] != 14)
		return dateStr;
	NSString * year = [dateStr substringToIndex:4];
	NSRange range;
	range.location = 4;
	range.length = 2;
	NSString * month = [dateStr substringWithRange:range];
	range.location = 6;
	NSString * day = [dateStr substringWithRange:range];
	range.location = 8;
	NSString * hour = [dateStr substringWithRange:range];
	range.location = 10;
	NSString * minute = [dateStr substringWithRange:range];
	range.location = 12;
	NSString * second = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@/%@/%@ %@:%@:%@", year, month, day, hour, minute, second];
}

+ (NSString *)formatFullMinuteFromDateStr:(NSString *)dateStr {
	// YYYYMMDDHHMISS
	if([dateStr length] != 14)
		return dateStr;
	NSString * year = [dateStr substringToIndex:4];
	NSRange range;
	range.location = 4;
	range.length = 2;
	NSString * month = [dateStr substringWithRange:range];
	range.location = 6;
	NSString * day = [dateStr substringWithRange:range];
	range.location = 8;
	NSString * hour = [dateStr substringWithRange:range];
	range.location = 10;
	NSString * minute = [dateStr substringWithRange:range];
	range.location = 12;
	NSString * second = [dateStr substringWithRange:range];
	return [NSString stringWithFormat:@"%@/%@/%@ %@:%@", year, month, day, hour, minute, second];
}

+ (NSString *)formatDate:(NSDate *)date withFormatString:(NSString *)format {
	NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    //[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"ko_KR"] autorelease]];
	[formatter setDateFormat:format];
	NSString * ret = [formatter stringFromDate:date];
	[formatter release];
	return ret;
}

+ (NSString *)nowFullDateStr {
	NSDate * now = [NSDate dateWithTimeIntervalSinceNow:0];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMddhhmmss"];
}

+ (NSString *)nowDateStr {
	NSDate * now = [NSDate dateWithTimeIntervalSinceNow:0];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMdd"];
}

+ (NSString *)nowTimeStr {
	NSDate * now = [NSDate dateWithTimeIntervalSinceNow:0];
	return [DateUtil formatDate:now withFormatString:@"HHmmss"];
}

+ (NSString *)prevDayDateStr:(int)prevDay{
	NSTimeInterval secondsPerDay = -((24 * 60 * 60)*prevDay);
	NSDate* now = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMdd"];	
}

+ (NSString *)prevDateStr:(int)prevMonth{
	NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSDate * now = [NSDate dateWithTimeIntervalSinceNow:0];
	NSDateComponents* prevDate = [calendar components:unitFlags fromDate:now];
	int year = [prevDate year];
	int month = [prevDate month];
	int day = [prevDate day];
	
	month = month - prevMonth;
	if(month < 1){
		year = year - 1;
		month = 12 + month;
	}
	[prevDate setYear:year];
	[prevDate setMonth:month];
	[prevDate setDay:day];
	
	now = [[NSCalendar currentCalendar] dateFromComponents:prevDate];
	[calendar release];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMdd"];	
}

+ (NSString *)nextDayDateStr:(int)nDay{
	NSTimeInterval secondsPerDay = ((24 * 60 * 60)*nDay);
	NSDate* now = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMdd"];	
}

+ (NSString *)nextDateStr:(int)nMonth{
	NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSDate * now = [NSDate dateWithTimeIntervalSinceNow:0];
	NSDateComponents* prevDate = [calendar components:unitFlags fromDate:now];
	int year = [prevDate year];
	int month = [prevDate month];
	int day = [prevDate day];
	
	month = month + nMonth;
	if(month > 12){
		year = year + 1;
		month = month - 12;
	}
	[prevDate setYear:year];
	[prevDate setMonth:month];
	[prevDate setDay:day];
	
	now = [[NSCalendar currentCalendar] dateFromComponents:prevDate];
	[calendar release];
	return [DateUtil formatDate:now withFormatString:@"yyyyMMdd"];	
}

+ (NSString *)lastDayOfMonth:(NSString *)day{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    // 포맷에 주의!
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:day];

    NSCalendar *c = [NSCalendar currentCalendar]; 
    NSRange days = [c rangeOfUnit:NSDayCalendarUnit  
                           inUnit:NSMonthCalendarUnit  
                          forDate:dateFromString];
    
    return [NSString stringWithFormat:@"%@%d",[day substringToIndex:8], days.length];
}

@end


